package cn.jane.bigevent.controller;

import cn.jane.bigevent.entity.Category;
import cn.jane.bigevent.entity.Result;
import cn.jane.bigevent.service.CategoryService;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 分类模块
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    private final CategoryService categoryService;

    public CategoryController(CategoryService categoryService) {
        this.categoryService = categoryService;
    }

    /**
     * 添加分类
     */
    @PostMapping
    public Result<Object> add(@Validated(Category.Add.class) @RequestBody Category category) {
        categoryService.add(category);
        return Result.success();
    }

    /**
     * 列举分类
     */
    @GetMapping
    public Result<List<Category>> list() {
        return Result.success(categoryService.list());
    }

    /**
     * 查询分类详情
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {
        return Result.success(categoryService.findById(id));
    }

    /**
     * 更新分类
     */
    @PutMapping
    public Result<Object> update(@Validated(Category.Update.class) @RequestBody Category category) {
        categoryService.update(category);
        return Result.success();
    }

    /**
     * 删除分类
     */
    @DeleteMapping
    public Result<Object> delete(Integer id) {
        categoryService.delete(id);
        return Result.success();
    }
}
